<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理</title>
    <link rel="stylesheet" href="./lib/bootstrap.css">
    <script src="./lib/jquery.js"></script>
</head>

<body>
    <!-- 添加图书的panel面板 -->

    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加图书</h3>
        </div>
        <div class="panel-body row">

            <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
                <div class="input-group ">
                    <div class="input-group-addon">书名</div>
                    <input type="text" class="form-control" id="bookname" placeholder="请输入书名">
                </div>
            </div>
            <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
                <div class="input-group">
                    <div class="input-group-addon">作者</div>
                    <input type="text" class="form-control" id="author" placeholder="请输入作者">
                </div>
            </div>
            <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
                <div class="input-group">
                    <div class="input-group-addon">出版社</div>
                    <input type="text" class="form-control" id="publisher" placeholder="请输入出版社">
                </div>
            </div>
            <button type="button" class="btn btn-primary" id="btnAdd">添加</button>

        </div>
    </div>

    <!-- 图书表格 -->

    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>id</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="tb">
            <tr>
                <td></td>
            </tr>
        </tbody>
    </table>

    <script>
        $(function() {
            getBookList();
            // 刷新图书列表
            function getBookList() {
                //1.发起Ajax请求
                $.get('http://www.liulongbin.top:3006/api/getbooks', function(res) {
                    console.log(res);
                    //2.获取数据是否成功
                    if (res.status !== 200) return console.warn('获取失败');
                    //3.渲染页面结构
                    let rows = [];
                    $.each(res.data, function(i, ele) { //将结构循环存入数组中
                        rows.push(`<tr><td>${ele.id}</td><td>${ele.bookname}</td><td>${ele.author}</td><td>${ele.publisher}</td><td><a href='javascript:;' class="del" data-id="${ele.id}">删除</a></td></tr>`)
                    });
                    $("#tb").empty().append(rows.join('')); //将保存结构的数组放进表格中
                })



            };


            //给动态生成的元素添加操作-->代理的方式
            $("#tb").on('click', '.del', function() {
                // console.log(11);
                let id = $(this).attr("data-id");
                console.log(id);
                $.get('http://www.liulongbin.top:3006/api/delbook', {
                    id: id
                }, function(res) {
                    if (res.status !== 200) return console.warn("删除失败" + res.status);
                    getBookList();
                })
            });

            $("#btnAdd").on('click', function() {
                let bookname = $("#bookname").val().trim(); //trim()可以去除字符串两端的空格
                let author = $("#author").val().trim();
                let publisher = $("#publisher").val().trim();
                if (bookname.lenght <= 0 || author <= 0 || publisher <= 0) {
                    return alert('请填写完整的图书信息');
                }

                $.post('http://www.liulongbin.top:3006/api/addbook', {
                    bookname: bookname,
                    author: author,
                    publisher: publisher
                }, function(res) {
                    if (res.status !== 201) {
                        return console.warn('添加图书失败');
                    }
                    getBookList();
                    $("#bookname").val('');
                    $("#author").val('');
                    $("#publisher").val('');
                })
            })








        })
    </script>
</body>

</html>